package com.cr.business.controller;

import com.cr.business.domain.TbProductInfo;
import com.cr.business.domain.dto.ProductReqDto;
import com.cr.business.service.ITbProductInfoService;
import com.cr.common.annotation.Log;
import com.cr.common.core.controller.BaseController;
import com.cr.common.core.domain.AjaxResult;
import com.cr.common.core.page.TableDataInfo;
import com.cr.common.enums.BusinessType;
import com.cr.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 产品信息Controller
 * 
 * @author tang
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/product/info")
public class TbProductInfoController extends BaseController
{
    @Resource
    private ITbProductInfoService tbProductInfoService;

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('product:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductReqDto dto)
    {
        startPage();
        List<TbProductInfo> list = tbProductInfoService.selectTbProductInfoList(dto);
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('product:info:export')")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductReqDto dto)
    {
        List<TbProductInfo> list = tbProductInfoService.selectTbProductInfoList(dto);
        ExcelUtil<TbProductInfo> util = new ExcelUtil<>(TbProductInfo.class);
        util.exportExcel(response, list, "产品信息数据");
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('product:info:add')")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid TbProductInfo tbProductInfo)
    {
        String username = getUsername();
        tbProductInfo.setCreateBy(username);
        tbProductInfo.setUpdateBy(username);
        return toAjax(tbProductInfoService.insertTbProductInfo(tbProductInfo));
    }

    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('product:info:remove')")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{proIds}")
    public AjaxResult remove(@PathVariable String[] proIds)
    {
        return toAjax(tbProductInfoService.deleteTbProductInfoByProIds(proIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('product:info:edit')")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TbProductInfo info)
    {
        info.setUpdateBy(getUsername());
        return toAjax(tbProductInfoService.updateTbProductInfo(info));
    }
}
